//----------------Sonic CD Menu Control Script----------------//
//--------Scripted by Christian Whitehead 'The Taxman'--------//
//-------Unpacked By Rubberduckycooly's Script Unpacker-------//

// Aliases
#alias Object.Value0			:	Object.Fade	// Also used as a timer

// Little Planet Aliases
#alias Object[32].Frame			:	LittlePlanet.Frame

// Music Selector Aliases
#alias Object[33].Frame			:	MusicSelector.Frame
#alias Object[33].XPos			:	MusicSelector.XPos
#alias Object[33].State			:	MusicSelector.State
#alias Object[33].DrawOrder		:	MusicSelector.DrawOrder
#alias Object[33].Priority		:	MusicSelector.Priority

// Touch Buttons Aliases
#alias Object[35].PropertyValue	:	Object.Button1Value
#alias Object[36].PropertyValue	:	Object.Button2Value
#alias Object[37].PropertyValue	:	Object.Button3Value

// States
#alias 0	:	MENUCONTROL_SETUP
#alias 1	:	MENUCONTROL_FADE_IN
#alias 2	:	MENUCONTROL_IDLE
#alias 3	:	MENUCONTROL_MUSIC_CONTROLS
#alias 4	:	MENUCONTROL_SET_MUSIC
#alias 5	:	MENUCONTROL_TIMETRAVEL
#alias 6	:	MENUCONTROL_EXIT

// Music Selector States
#alias 0	:	MUSICSELECTOR_BLANK
#alias 1	:	MUSICSELECTOR_STOP_MOVE_L
#alias 2	:	MUSICSELECTOR_MOVE_L
#alias 4	:	MUSICSELECTOR_MOVE_R

// Little Planet Sprite Frame Aliases
#alias 0	:	LPFRAME_PRESENT
#alias 11	:	LPFRAME_GOODFUTURE
#alias 22	:	LPFRAME_BADFUTURE

// Priority
#alias 0	:	PRIORITY_BOUNDS
#alias 1	:	PRIORITY_ACTIVE

// Presentation Stage
#alias 1	:	STAGE_P_MENU

// Device Type
#alias 1	:	RETRO_MOBILE

// Function Declarations
#function MenuControl_ChangeTrack


function MenuControl_ChangeTrack
	switch MusicSelector.Frame
	case 0
		SetMusicTrack("JP/R1A.ogg", 0, 142428)
		SetMusicTrack("US/R1A.ogg", 1, 153038)
		LittlePlanet.Frame = LPFRAME_PRESENT
		break

	case 1
		SetMusicTrack("JP/R1C.ogg", 0, 142902)
		SetMusicTrack("US/R1C.ogg", 1, 152346)
		LittlePlanet.Frame = LPFRAME_GOODFUTURE
		break

	case 2
		SetMusicTrack("JP/R1D.ogg", 0, 1)
		SetMusicTrack("US/R1D.ogg", 1, 774998)
		LittlePlanet.Frame = LPFRAME_BADFUTURE
		break

	case 3
		switch SaveRam[513]
			case 0 // Original
				SetMusicTrack("JP/R2A.ogg", 0, true)
				SetMusicTrack("JP/R2A.ogg", 1, true)
				break
			
			case 1 // US
				SetMusicTrack("US/R2A.ogg", 0, true)
				SetMusicTrack("US/R2A.ogg", 1, true)
				break
			
			case 2 // EU
				SetMusicTrack("ALT1/R2A.ogg", 0, true)
				SetMusicTrack("ALT1/R2A.ogg", 1, true)
				break
				
			case 3 // Alt. Mix 0
				SetMusicTrack("ALT2/R2A.ogg", 0, true)
				SetMusicTrack("ALT2/R2A.ogg", 1, true)
				break
		end switch
		LittlePlanet.Frame = LPFRAME_PRESENT
		break

	case 4
		switch SaveRam[515]
			case 0 // JP
				SetMusicTrack("JP/R2C.ogg", 0, true)
				SetMusicTrack("JP/R2C.ogg", 1, true)
				break
				
			case 1 // US
				SetMusicTrack("US/R2C.ogg", 0, true)
				SetMusicTrack("US/R2C.ogg", 1, true)
				break
			
			case 2 // EU
				SetMusicTrack("ALT1/R2C.ogg", 0, true)
				SetMusicTrack("ALT1/R2C.ogg", 1, true)
				break
			
			case 3 // Alt. Mix 0
				SetMusicTrack("ALT2/R2C.ogg", 0, true)
				SetMusicTrack("ALT2/R2C.ogg", 1, true)
				break
				
			case 4 // Sonic Stream
				SetMusicTrack("ALT3/R2C_SonicStream.ogg", 0, true)
				SetMusicTrack("ALT3/R2C_SonicStream.ogg", 1, true)
				break
		end switch
		LittlePlanet.Frame = LPFRAME_GOODFUTURE
		break

	case 5
		switch SaveRam[516]
			case 0 // JP
				SetMusicTrack("JP/R2D.ogg", 0, true)
				SetMusicTrack("JP/R2D.ogg", 1, true)
				break
			
			case 1 //US
				SetMusicTrack("US/R2D.ogg", 0, true)
				SetMusicTrack("US/R2D.ogg", 1, true)
				break
			
			case 2 // EU
				SetMusicTrack("ALT1/R2D.ogg", 0, true)
				SetMusicTrack("ALT1/R2D.ogg", 1, true)
				break
				
			case 3 // Alt. Mix0
				SetMusicTrack("ALT2/R2D.ogg", 0, true)
				SetMusicTrack("ALT2/R2D.ogg", 1, true)
				break
				
			case 4 // Alt.Mix 1
				SetMusicTrack("ALT3/R2D_colterex.ogg", 0, true)
				SetMusicTrack("ALT3/R2D_colterex.ogg", 1, true)
				break
		end switch
		LittlePlanet.Frame = LPFRAME_BADFUTURE
		break

	case 6
		SetMusicTrack("JP/R3A.ogg", 0, 358725)
		SetMusicTrack("US/R3A.ogg", 1, 1)
		LittlePlanet.Frame = LPFRAME_PRESENT
		break

	case 7
		SetMusicTrack("JP/R3C.ogg", 0, 1)
		SetMusicTrack("US/R3C.ogg", 1, 173494)
		LittlePlanet.Frame = LPFRAME_GOODFUTURE
		break
		
	case 8
		SetMusicTrack("JP/R3D.ogg", 0, 717648)
		SetMusicTrack("US/R3D.ogg", 1, 1)
		LittlePlanet.Frame = LPFRAME_BADFUTURE
		break

	case 9
		SetMusicTrack("JP/R4A.ogg", 0, 1)
		SetMusicTrack("US/R4A.ogg", 1, 415148)
		LittlePlanet.Frame = LPFRAME_PRESENT
		break

	case 10
		SetMusicTrack("JP/R4C.ogg", 0, 1)
		SetMusicTrack("US/R4C.ogg", 1, 1)
		LittlePlanet.Frame = LPFRAME_GOODFUTURE
		break

	case 11
		SetMusicTrack("JP/R4D.ogg", 0, 1)
		SetMusicTrack("US/R4D.ogg", 1, 145616)
		LittlePlanet.Frame = LPFRAME_BADFUTURE
		break

	case 12
		switch SaveRam[543]
			case 0 // JP
				SetMusicTrack("JP/R4.5A.ogg", 0, true)
				SetMusicTrack("JP/R4.5A.ogg", 1, true)
				break
			
			case 1 // US
				SetMusicTrack("US/R4.5A.ogg", 0, true)
				SetMusicTrack("US/R4.5A.ogg", 1, true)
				break
			
			case 2 // Alt. Mix 1
				SetMusicTrack("ALT1/R4.5A.ogg", 0, true)
				SetMusicTrack("ALT1/R4.5A.ogg", 1, true)
				break
				
			case 3 // Alt. Mix 2
				SetMusicTrack("ALT2/R4.5A.ogg", 0, true)
				SetMusicTrack("ALT2/R4.5A.ogg", 1, true)
				break
		end switch
		LittlePlanet.Frame = LPFRAME_PRESENT
		break

	case 13
		switch SaveRam[545]
			case 0 // JP
				SetMusicTrack("JP/R4.5C.ogg", 0, true)
				SetMusicTrack("JP/R4.5C.ogg", 1, true)
				break
				
			case 1 // US
				SetMusicTrack("US/R4.5C.ogg", 0, true)
				SetMusicTrack("US/R4.5C.ogg", 1, true)
				break
			
			case 2 // Alt. Mix 1
				SetMusicTrack("ALT1/R4.5C.ogg", 0, true)
				SetMusicTrack("ALT1/R4.5C.ogg", 1, true)
				break
			
			case 3 // Alt. Mix 2
				SetMusicTrack("ALT2/R4.5C.ogg", 0, true)
				SetMusicTrack("ALT2/R4.5C.ogg", 1, true)
				break
		end switch
		LittlePlanet.Frame = LPFRAME_GOODFUTURE
		break

	case 14
		switch SaveRam[546]
			case 0 // JP
				SetMusicTrack("JP/R4.5D.ogg", 0, true)
				SetMusicTrack("JP/R4.5D.ogg", 1, true)
				break
			
			case 1 //US
				SetMusicTrack("US/R4.5D.ogg", 0, true)
				SetMusicTrack("US/R4.5D.ogg", 1, true)
				break
			
			case 2 // Alt. Mix 1
				SetMusicTrack("ALT1/R4.5D.ogg", 0, true)
				SetMusicTrack("ALT1/R4.5D.ogg", 1, true)
				break
				
			case 3 // Alt. Mix 2
				SetMusicTrack("ALT2/R4.5D.ogg", 0, true)
				SetMusicTrack("ALT2/R4.5D.ogg", 1, true)
				break
		end switch
		LittlePlanet.Frame = LPFRAME_BADFUTURE
		break

	case 15
		SetMusicTrack("JP/R5A.ogg", 0, 1)
		SetMusicTrack("US/R5A.ogg", 1, 1)
		LittlePlanet.Frame = LPFRAME_PRESENT
		break

	case 16
		SetMusicTrack("JP/R5C.ogg", 0, 367046)
		SetMusicTrack("US/R5C.ogg", 1, 705830)
		LittlePlanet.Frame = LPFRAME_GOODFUTURE
		break

	case 17
		SetMusicTrack("JP/R5D.ogg", 0, 1)
		SetMusicTrack("US/R5D.ogg", 1, 93672)
		LittlePlanet.Frame = LPFRAME_BADFUTURE
		break

	case 18
		SetMusicTrack("JP/R6A.ogg", 0, 1)
		SetMusicTrack("US/R6A.ogg", 1, 926048)
		LittlePlanet.Frame = LPFRAME_PRESENT
		break

	case 19
		SetMusicTrack("JP/R6C.ogg", 0, 1)
		SetMusicTrack("US/R6C.ogg", 1, 181704)
		LittlePlanet.Frame = LPFRAME_GOODFUTURE
		break

	case 20
		SetMusicTrack("JP/R6D.ogg", 0, 1)
		SetMusicTrack("US/R6D.ogg", 1, 45355)
		LittlePlanet.Frame = LPFRAME_BADFUTURE
		break

	case 21
		SetMusicTrack("JP/R7A.ogg", 0, 1)
		SetMusicTrack("US/R7A.ogg", 1, 617728)
		LittlePlanet.Frame = LPFRAME_PRESENT
		break

	case 22
		SetMusicTrack("JP/R7C.ogg", 0, 103060)
		SetMusicTrack("US/R7C.ogg", 1, 136670)
		LittlePlanet.Frame = LPFRAME_GOODFUTURE
		break

	case 23
		SetMusicTrack("JP/R7D.ogg", 0, 1)
		SetMusicTrack("US/R7D.ogg", 1, 92324)
		LittlePlanet.Frame = LPFRAME_BADFUTURE
		break

	case 24
		SetMusicTrack("JP/R8A.ogg", 0, 146030)
		SetMusicTrack("US/R8A.ogg", 1, 1)
		LittlePlanet.Frame = LPFRAME_PRESENT
		break

	case 25
		SetMusicTrack("JP/R8C.ogg", 0, 1)
		SetMusicTrack("US/R8C.ogg", 1, 1)
		LittlePlanet.Frame = LPFRAME_GOODFUTURE
		break

	case 26
		SetMusicTrack("JP/R8D.ogg", 0, 1)
		SetMusicTrack("US/R8D.ogg", 1, 563962)
		LittlePlanet.Frame = LPFRAME_BADFUTURE
		break

	case 27
		SetMusicTrack("JP/FinalBoss.ogg", 0, 1)
		SetMusicTrack("US/FinalBoss.ogg", 1, 1)
		LittlePlanet.Frame = LPFRAME_PRESENT
		break

	case 28
		SetMusicTrack("JP/DAGarden.ogg", 0, 1)
		SetMusicTrack("US/DAGarden.ogg", 1, 117382)
		LittlePlanet.Frame = LPFRAME_PRESENT
		break

	case 29
		SetMusicTrack("JP/GameOver.ogg", 0, 0)
		SetMusicTrack("US/GameOver.ogg", 1, 0)
		LittlePlanet.Frame = LPFRAME_PRESENT
		break

	case 30
		SetMusicTrack("JP/ZoneComplete.ogg", 0, 0)
		SetMusicTrack("US/ZoneComplete.ogg", 1, 0)
		LittlePlanet.Frame = LPFRAME_PRESENT
		break

	case 31
		SetMusicTrack("JP/Boss.ogg", 0, 1)
		SetMusicTrack("US/Boss.ogg", 1, 196526)
		LittlePlanet.Frame = LPFRAME_PRESENT
		break

	case 32
		SetMusicTrack("JP/Invincibility.ogg", 0, 0)
		SetMusicTrack("US/Invincibility.ogg", 1, 0)
		LittlePlanet.Frame = LPFRAME_PRESENT
		break

	case 33
		SetMusicTrack("JP/SpeedShoes.ogg", 0, 0)
		SetMusicTrack("US/SpeedShoes.ogg", 1, 0)
		LittlePlanet.Frame = LPFRAME_PRESENT
		break

	case 34
		SetMusicTrack("JP/TitleScreen.ogg", 0, 0)
		SetMusicTrack("US/TitleScreen.ogg", 1, 0)
		LittlePlanet.Frame = LPFRAME_PRESENT
		break

	case 35
		SetMusicTrack("JP/SpecialStage.ogg", 0, 1)
		SetMusicTrack("US/SpecialStage.ogg", 1, 270972)
		LittlePlanet.Frame = LPFRAME_PRESENT
		break

	case 36
		SetMusicTrack("JP/TimeAttack.ogg", 0, 100512)
		SetMusicTrack("US/DAGarden.ogg", 1, 117382)
		LittlePlanet.Frame = LPFRAME_PRESENT
		break

	end switch

	PlayMusic(Options.Soundtrack)
end function


sub ObjectMain
	switch Object.State
	case MENUCONTROL_SETUP
		Object.State = MENUCONTROL_FADE_IN
		Object.Fade	 = 384
		SetScreenFade(0, 0, 0, 255)
		PlayMusic(Options.Soundtrack)
		break

	case MENUCONTROL_FADE_IN
		if Object.Fade > 0
			Object.Fade -= 8
		else
			Object.State = MENUCONTROL_IDLE
		end if
		SetScreenFade(0, 0, 0, Object.Fade)
		break

	case MENUCONTROL_IDLE
		// Previously Start, Origins change
		if KeyPress[0].ButtonB == true
			StopMusic()
			Object.State = MENUCONTROL_EXIT
		end if

		if KeyPress[0].ButtonA == true
			Object.State = MENUCONTROL_MUSIC_CONTROLS
			MusicSelector.XPos   = Screen.XSize
			MusicSelector.XPos  += 96
			MusicSelector.XPos <<= 16

			MusicSelector.State		= MUSICSELECTOR_STOP_MOVE_L
			MusicSelector.DrawOrder = 5
			MusicSelector.Priority	= PRIORITY_ACTIVE

			Object.Button1Value = 4
			Object.Button2Value = 5
			Object.Button3Value = 6
		end if
		break

	case MENUCONTROL_MUSIC_CONTROLS
		if MusicSelector.State == MUSICSELECTOR_BLANK
			if KeyPress[0].Left == true
				MusicSelector.State = MUSICSELECTOR_MOVE_L
			end if

			if KeyPress[0].Right == true
				MusicSelector.State = MUSICSELECTOR_MOVE_R
			end if

			if KeyPress[0].ButtonA == true
				Object.State = MENUCONTROL_SET_MUSIC
				StopMusic()
			end if

			// Previously ButtonB, Origins change
			if KeyPress[0].Start == true
				Object.State = MENUCONTROL_SET_MUSIC
				StopMusic()
			end if

			if KeyPress[0].ButtonC == true
				Object.State = MENUCONTROL_SET_MUSIC
				StopMusic()
			end if
		end if

		// Previously Start, Origins change
		if KeyPress[0].ButtonB == true
			StopMusic()
			Object.State = MENUCONTROL_EXIT
		end if
		break

	case MENUCONTROL_SET_MUSIC
		if Object.Fade < 288
			Object.Fade += 8
		else
			MusicSelector.XPos   = Screen.XSize
			MusicSelector.XPos  += 96
			MusicSelector.XPos <<= 16

			MusicSelector.State		= MUSICSELECTOR_BLANK
			MusicSelector.Priority	= PRIORITY_BOUNDS

			Object.Button1Value = 0
			Object.Button2Value = 2
			Object.Button3Value = 7

			CallFunction(MenuControl_ChangeTrack)
			Object.State++
		end if
		SetScreenFade(208, 255, 224, Object.Fade)
		break

	case MENUCONTROL_TIMETRAVEL
		if Object.Fade > 0
			Object.Fade -= 8
		else
			Object.State = MENUCONTROL_IDLE
		end if
		SetScreenFade(208, 255, 224, Object.Fade)
		break

	case MENUCONTROL_EXIT
		if Object.Fade < 384
			Object.Fade += 8
		else
			// Return to the Menu scene
			Stage.ListPos = STAGE_P_MENU
			LoadStage()
		end if
		SetScreenFade(0, 0, 0, Object.Fade)
		break

	end switch
end sub


sub ObjectStartup
	SetMusicTrack("JP/DAGarden.ogg", 0, 1)
	SetMusicTrack("US/DAGarden.ogg", 1, 117382)
	Object[0].Type = TypeName[Menu Control]
	Object[0].DrawOrder = 1
	Object[0].Priority = PRIORITY_ACTIVE

#platform: Use_Origins
	if Engine.DeviceType == RETRO_MOBILE
		Object[34].DrawOrder = 5
		Object[35].DrawOrder = 5
		Object[36].DrawOrder = 5
		Object[37].DrawOrder = 5

		TempValue0   = Screen.XSize
		TempValue0 >>= 2

		TempValue1   = TempValue0
		TempValue1 >>= 1
		Object[34].iXPos = TempValue1

		TempValue1 += TempValue0
		Object[35].iXPos = TempValue1

		TempValue1 += TempValue0
		Object[36].iXPos = TempValue1

		TempValue1 += TempValue0
		Object[37].iXPos = TempValue1
	else
		Object[34].Type = TypeName[Blank Object]
		Object[35].Type = TypeName[Blank Object]
		Object[36].Type = TypeName[Blank Object]
		Object[37].Type = TypeName[Blank Object]
	end if
#endplatform

#platform: Use_Standalone
	#platform: Mobile
		// Setup all the Touch Buttons for the player to tap
		Object[34].DrawOrder = 5
		Object[35].DrawOrder = 5
		Object[36].DrawOrder = 5
		Object[37].DrawOrder = 5

		TempValue0   = Screen.XSize
		TempValue0 >>= 2

		TempValue1   = TempValue0
		TempValue1 >>= 1
		Object[34].iXPos = TempValue1

		TempValue1  += TempValue0
		Object[35].iXPos = TempValue1

		TempValue1  += TempValue0
		Object[36].iXPos = TempValue1

		TempValue1  += TempValue0
		Object[37].iXPos = TempValue1
	#endplatform
#endplatform

#platform: Use_Standalone
	#platform: Standard
		// Standard platforms don't need Touch Buttons since actual physical buttons are available to the player
		Object[34].Type = TypeName[Blank Object]
		Object[35].Type = TypeName[Blank Object]
		Object[36].Type = TypeName[Blank Object]
		Object[37].Type = TypeName[Blank Object]
	#endplatform
#endplatform
end sub


// ========================
// Editor Subs
// ========================

sub RSDKDraw
	DrawSprite(0)
end sub


sub RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)		// #0 - "Script" Icon

	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end sub
